<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
include('codexcontroller.php');

class CRUD extends codexController{
    function CRUD(){
        codexcontroller::codexcontroller();
        /*
        if(!$this->codexsession->userdata('codex_crud_table')){
            if($this->db->table_exists($this->uri->segment(3)) AND !in_array($this->uri->segment(3),$this->config->item('codex_auto_generate_exclude_tables'))){
                $table = $this->uri->segment(3);
                $this->codexsession->set_userdata('codex_crud_table',$table);
            }
            else
                show_error('Table generation for '.$this->uri->segment(3).' not allowed.');
        }
        else
            if($this->db->table_exists($this->uri->segment(3)) AND !in_array($this->uri->segment(3),$this->config->item('codex_auto_generate_exclude_tables'))
                    AND $this->codexsession->userdata('codex_crud_table') != $this->uri->segment(3)){
                $table = $this->uri->segment(3);
                $this->codexsession->set_userdata('codex_crud_table',$table);
            }
            else
                $table = $this->codexsession->userdata('codex_crud_table');
         */

        if(array_key_exists('t',$_GET)){
            $table = $_GET['t'];

            if(!($this->db->table_exists($table) && !in_array($table,$this->config->item('codex_auto_generate_exclude_tables')))){
                show_error('Table generation for '.$table.' not allowed.');
            }
        }
        else
            show_error('Table name missing.');

        if(file_exists(APPPATH.$this->config->item('codex_definitions_dir').$table.'.yml')){
            $this->load->library('spyc');
            $config = $this->spyc->YAMLLOAD($this->config->item('codex_definitions_dir').$table.'.yml');
        }
        else
            $config = array(
                          'db_table'=>$table,
                          'form_setup'=>$this->codexforms->getSetupForTable($table),
                          'controller_name'=>'CRUD',
                          'page_header'=>humanize($table)
                      );

        // Setup the special links 
        $get_array = $_GET;

        $get_array['m'] = 'index';
        $config['controller_link'] = '?'.$this->implodeAssoc('=','&amp;',$get_array);
        $get_array = $_GET;

        $get_array['m'] = 'execute_add';
        $config['add_action'] = '?'.$this->implodeAssoc('=','&amp;',$get_array);
        $get_array = $_GET;

        $config['edit_action'] = '?'.$this->implodeAssoc('=','&amp;',array('c'=>'CRUD','m'=>'execute_edit','t'=>$table));
        $config['delete_action'] = '?'.$this->implodeAssoc('=','&amp;',array('c'=>'CRUD','m'=>'manage','a'=>'delete_selected','t'=>$table));

        $get_array['m'] = 'manage';
        $get_array['a'] = 'edit';
        $get_array['id'] = '{num}';
        $config['edit_link'] = '?'.$this->implodeAssoc('=','&amp;',$get_array);
        $get_array = $_GET;

        $get_array['m'] = 'add';
        $config['add_link'] = '?'.$this->implodeAssoc('=','&amp;',$get_array);
        $get_array = $_GET;

        $this->setConfig($config);
    }
    function show(){
        $this->index();
    }
    function manage(){
        $action = $_GET['a'];
        $id = (isset($_GET['id']))? $_GET['id'] : '';

        if($action == 'edit')
            return $this->_edit($id);

        else if($action == 'delete')
            return $this->delete_confirm($id);
        
        else if($action == 'delete_selected'){
            $selected_rows = $this->input->post("selected_rows");

            if(is_array($selected_rows)){
                foreach($selected_rows as $id){
                    $this->_delete_selected_helper($id);
                }
            }
        }
        $this->index();
    }
    function implodeAssoc($glue_inner,$glue_outer,$array){
        $count = count($array);
        $output = '';
        $i = 0;

        foreach($array as $k=>$v){
            $output .= $k.$glue_inner.$v;

            if($i != $count - 1)
                $output .= $glue_outer;
            $i++;
        }
        return $output;
    }
}
?>
